
package com.netease.xone.widget.richtext;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.style.ImageSpan;
import android.util.AttributeSet;

import com.netease.xone.R;
import com.netease.xone.widget.richtext.span.FaceSpanManager;
import com.netease.xone.widget.richtext.span.ImageAudioSpan;
import com.netease.xone.widget.richtext.span.LinkSpan;
import com.netease.xone.widget.richtext.span.NickNameSpan;
import com.netease.xone.widget.richtext.span.SubjectSpan;
import com.netease.xone.widget.richtext.span.TopicSpan;

public class DiscussTextView extends BaseRichTextView {

    public static final String SUBJECT_ICON_REPLACE = "I";// 主题图标替代
    
    private int mEnable = IRichTextClickListener.ENABLE_RICHTEXT_MASK;
    private IRichTextClickListener mListener;

    public DiscussTextView(Context context) {
        this(context, null, 0);
    }

    public DiscussTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DiscussTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setMovementMethod(LinkMovementMethod.getInstance());
        addMatcher(SubjectSpan.REGULAR_SUBJECT);
        addMatcher(NickNameSpan.REGULAR_NICKNAME);
        addMatcher(TopicSpan.REGULAR_TOPIC);
        addMatcher(FaceSpanManager.REGULAR_FACE);
        addMatcher(ImageAudioSpan.REGULAR_IMAGE_AUDIO);
        addMatcher(LinkSpan.REGULAR_LINK);
    }

    @Override
    public void setRichText(CharSequence str) {
        // 预处理，首先在主题前插入图标替代
        setText(str, BufferType.SPANNABLE);
        Spannable sp = (Spannable)getText();
        Pattern p = Pattern.compile(SubjectSpan.REGULAR_SUBJECT);
        Matcher matcher = p.matcher(sp);
        List<Integer> indexList = new ArrayList<Integer>();
        // 查找主题开始的位置
        while (matcher.find()) {
            int start = matcher.start();
            indexList.add(start);
        }
        StringBuffer sb = new StringBuffer(str);
        for (int i = 0; i < indexList.size(); i++) {
            // 在主题*过后第一个位置插入
            sb.insert(indexList.get(i) + 1, SUBJECT_ICON_REPLACE);
        }
        // 插入完毕
        super.setRichText(sb.toString());
    }

    @Override
    public void onMatched(String regularExp, int start, int end) {
        Spannable sp = (Spannable)getText();
        if (regularExp.equals(NickNameSpan.REGULAR_NICKNAME) 
                && (mEnable&IRichTextClickListener.ENABLE_RICHTEXT_NAME) == IRichTextClickListener.ENABLE_RICHTEXT_NAME) {
            sp.setSpan(new NickNameSpan(sp.subSequence(start, end).toString(), mListener), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (regularExp.equals(TopicSpan.REGULAR_TOPIC)
                && (mEnable&IRichTextClickListener.ENABLE_RICHTEXT_TOPIC) == IRichTextClickListener.ENABLE_RICHTEXT_TOPIC) {
            sp.setSpan(new TopicSpan(sp.subSequence(start, end).toString(), mListener), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (regularExp.equals(SubjectSpan.REGULAR_SUBJECT)
                && (mEnable&IRichTextClickListener.ENABLE_RICHTEXT_SUBJECT) == IRichTextClickListener.ENABLE_RICHTEXT_SUBJECT) {
            sp.setSpan(new SubjectSpan(sp.subSequence(start, end).toString(), mListener), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // 替换为图标start + 1表示从*过后第一个字符开始
            sp.setSpan(new ImageSpan(getContext(), R.drawable.test_icon_wuhun), start + 1, start + 1 + SUBJECT_ICON_REPLACE.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (regularExp.equals(FaceSpanManager.REGULAR_FACE)
                && (mEnable&IRichTextClickListener.ENABLE_RICHTEXT_FACE) == IRichTextClickListener.ENABLE_RICHTEXT_FACE) {
            FaceSpanManager.parseFace(getContext(), sp, sp.subSequence(start, end).toString(),
                    start, end);
        } else if (regularExp.equals(ImageAudioSpan.REGULAR_IMAGE_AUDIO)
                && (mEnable&IRichTextClickListener.ENABLE_RICHTEXT_IMAGE_AUDIO) == IRichTextClickListener.ENABLE_RICHTEXT_IMAGE_AUDIO) {
            new ImageAudioSpan(sp.subSequence(start, end).toString(), mListener).setSpan(getContext(), sp, start, end);
        } else if (regularExp.equals(LinkSpan.REGULAR_LINK)
                && (mEnable&IRichTextClickListener.ENABLE_RICHTEXT_LINK) == IRichTextClickListener.ENABLE_RICHTEXT_LINK) {
            sp.setSpan(new LinkSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    
    public void setEnable(int enable) {
        mEnable = mEnable&enable;
    }
    
    public void setRichTextClickListener(IRichTextClickListener listener){
        mListener = listener;
    }
}
